package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDTO;
import com.zzyl.dto.NursingProjectPlanDTO;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVO;
import com.zzyl.vo.NursingProjectPlanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    /**
     * 查询所有护理计划
     * @return
     */
    @Override
    public List<NursingPlanVO> getAll() {
        List<NursingPlan> nursingPlans = nursingPlanMapper.getAll();
        List<NursingPlanVO> nursingPlanVOS = new ArrayList<>();
        for (int i = 0; i < nursingPlans.size(); i++) {
            NursingPlanVO nursingPlanVO = BeanUtil.toBean(nursingPlans.get(i),NursingPlanVO.class);
            nursingPlanVOS.add(nursingPlanVO);
        }
        return nursingPlanVOS;
    }

    /**
     * 根据id查询护理计划
     * @param id
     * @return
     */
    @Override
    public NursingPlanVO getById(Long id) {
        return nursingPlanMapper.getById(id);
    }

    /**
     * 新增护理计划
     * @param nursingPlanDTO
     */
    @Override
    @Transactional
    public void insert(NursingPlanDTO nursingPlanDTO) {
        //护理项目为空，提示“护理项目信息不完整，请重新设置”
        // if(BeanUtil.isEmpty(nursingPlanDTO.getProjectPlans())){
        //     System.out.println("护理项目信息不完整，请重新设置");
        // }

        // 新增护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDTO,NursingPlan.class);
        List<NursingProjectPlanDTO> projectPlans = nursingPlanDTO.getProjectPlans();
        List<NursingProjectPlan> nursingProjectPlans = new ArrayList<>();
        for (NursingProjectPlanDTO projectPlan : projectPlans) {
            nursingProjectPlans.add(BeanUtil.toBean(projectPlan,NursingProjectPlan.class));
        }
        nursingPlan.setNursingProjectPlans(nursingProjectPlans);

        nursingPlanMapper.insertPlan(nursingPlan);

        // 设置planId
        // List<NursingProjectPlan> nursingProjectPlans = nursingPlan.getNursingProjectPlans();
        for (NursingProjectPlan nursingProjectPlan : nursingProjectPlans) {
            nursingProjectPlan.setPlanId(nursingPlan.getId());
        }

        // 新增护理项目-计划
        nursingPlanMapper.insertProjectPlan(nursingProjectPlans);
    }

    /**
     * 条件分页查询
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingPlanVO> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<NursingPlanVO> nursingPlanVOS = nursingPlanMapper.getByPage(name,status);
        for (NursingPlanVO nursingPlanVO : nursingPlanVOS) {
            Integer count = nursingPlanMapper.getCount(nursingPlanVO.getId());
            nursingPlanVO.setCount(count);
        }
        Page<NursingPlanVO> page = (Page<NursingPlanVO>) nursingPlanVOS;
        return PageResponse.of(page,NursingPlanVO.class);
    }
}
